#include<vector>
using namespace std;
class Solution {
public:
    int candy(vector<int>& ratings) {
        int des = 1;
        int prev = 0;
        int des1 = ratings.size() - 2;
        int prev1 = ratings.size() - 1; 
        int count = ratings.size();
        vector<int> v(ratings.size(), 1);
        while (des1>=0 && des<=ratings.size()-1)
        {
            if (ratings[des] > ratings[prev] && v[des] <= v[prev])
            {
                int tem = v[des];
                v[des] = v[prev] + 1;
                count += v[des] - tem;
            }
            if (ratings[prev] > ratings[des] && v[prev] <= v[des])
            {
                int tem = v[prev];
                v[prev] = v[des] + 1;
                count += v[prev] - tem;
            }
            if (ratings[des1] > ratings[prev1] && v[des1] <= v[prev1])
            {
                int tem = v[des1];
                v[des1] = v[prev1] + 1;
                count += v[des1] - tem;
            }
            if (ratings[prev1] > ratings[des1] && v[prev1] <= v[des1])
            {
                int tem = v[prev1];
                v[prev1] = v[des1] + 1;
                count += v[prev1] - tem;
            }
            //if (ratings[prev] == ratings[des] && v[des] < v[prev])
            //{
            //    int tem = v[des];
            //    v[des] = v[prev];
            //    count += v[des] - tem;
            //}
            des++;
            prev++;
            des1--;
            prev1--;
        }
        return count;
    }
};

int main()
{
    Solution a;
    vector<int> v = {1,6,10,8,7,3,2};
    a.candy(v);
    return 0;
}